//
//  LRXMoreThingInfoCell.m
//  路人侠
//
//  Created by zhangxulong on 3/7/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXMoreThingInfoCell.h"
@interface LRXMoreThingInfoCell()
@property (nonatomic,strong) UILabel * thingName;
@property (nonatomic,strong) UILabel * thingMoney;
@property (nonatomic,strong) UILabel * thingCount;
@property (nonatomic,strong) UIButton * addCount;
@property (nonatomic,strong) UIButton * redCount;
@end


@implementation LRXMoreThingInfoCell
- (UILabel *)thingName{
    if (_thingName == nil) {
        _thingName = [[UILabel alloc]init];
    }
    return _thingName;
}
- (UILabel *)thingMoney{
    if (_thingMoney == nil) {
        _thingMoney = [[UILabel alloc]init];
    }
    return _thingMoney;
}
- (UILabel *)thingCount{
    if (_thingCount == nil) {
        _thingCount = [[UILabel alloc]init];
    }
    return _thingCount;
}
- (UIButton *)addCount{
    if (_addCount == nil) {
        _addCount = [UIButton buttonWithType:UIButtonTypeContactAdd];

    }
    return _addCount;
}
- (UIButton *)redCount{
    if (_redCount == nil) {
        _redCount = [UIButton buttonWithType:UIButtonTypeContactAdd];
    }
    return _redCount;
}

- (void)layoutMoreThing:(LRXThing *)thing
{
    self.thingName.text = thing.thingName;
    self.thingMoney.text = [NSString stringWithFormat:@"%@元", thing.thingMoney];
    self.thingCount.text = thing.thingCount;
    

    self.thingName.textColor = [UIColor colorWithRed:131 green:131 blue:131];
    self.thingName.font = [UIFont systemFontOfSize:28 * hScale];
    self.thingName.textAlignment = NSTextAlignmentLeft;
    self.thingName.frame = CGRectMake(10 * wScale, 30 * hScale, 150 * wScale, 30 * hScale);
    
    
    self.thingMoney.textAlignment = NSTextAlignmentCenter;
    self.thingMoney.textColor = [UIColor colorWithRed:182 green:182 blue:182];
    self.thingMoney.font = self.thingName.font;
  
    self.thingMoney.frame = CGRectMake((MainScreenW - 100 * wScale) * 0.5 - 75 * wScale, 30 * hScale, 150 * wScale, 32 * hScale);
    
    
    
    
   
    self.thingCount.textColor = mainColor;
    self.thingCount.font = [UIFont systemFontOfSize:23 * hScale];
    self.thingCount.bounds = CGRectMake(0 , 0 , 40 * wScale, 26 * hScale);
    self.thingCount.center = CGPointMake(MainScreenW / 4 * 3, 50 *hScale);
    self.thingCount.textAlignment = NSTextAlignmentCenter;
    
    
    
    [self.addCount setImage:[UIImage imageNamed:@"jiahao"] forState:UIControlStateNormal];
    [self.redCount setImage:[UIImage imageNamed:@"jianhao"] forState:UIControlStateNormal];
    
  
    
    self.redCount.frame = CGRectMake(CGRectGetMinX(self.thingCount.frame) - 40 * wScale, 34 *hScale, 35 *hScale, 35 *hScale);
    self.addCount.frame = CGRectMake(CGRectGetMaxX(self.thingCount.frame) + 5 * wScale, 34 * hScale, 35 * hScale, 35 *hScale);
    
    
    [self.addCount addTarget:self action:@selector(addCountNum) forControlEvents:UIControlEventTouchUpInside];
    [self.redCount addTarget:self action:@selector(redCountNum) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.addCount];
    [self addSubview: self.redCount];
    [self addSubview:self.thingMoney];
    [self addSubview:self.thingCount];
    [self addSubview:self.thingName];
}

- (void)addCountNum{
    
    
    NSString * countNum = self.thingCount.text;
    NSInteger pcountNum = [countNum integerValue] + 1;
    
    if (pcountNum > 0) {
        self.redCount.hidden = NO;
        self.thingCount.hidden = NO;

    }
    self.thingCount.text = [NSString stringWithFormat:@"%ld",pcountNum];
    
    if ([self.cellUpateDelegate respondsToSelector:@selector(updateCellInfo:withCount:)]) {
        [self.cellUpateDelegate updateCellInfo:self.cellIndexPath withCount:pcountNum];
    }
    
}
- (void)redCountNum{
    NSString * countNum = self.thingCount.text;
    NSInteger pcountNum = [countNum integerValue] - 1;
    
    if (pcountNum <= 0) {
        self.redCount.hidden = YES;
        self.thingCount.hidden = YES;
    }
    
    if ([self.cellUpateDelegate respondsToSelector:@selector(updateCellInfo:withCount:)]) {
        [self.cellUpateDelegate updateCellInfo:self.cellIndexPath withCount:pcountNum];
    }
    self.thingCount.text = [NSString stringWithFormat:@"%ld",pcountNum];
    
    
}
@end
